package com.rain.shoppingscienceproj.ui.activity.search;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.http.BaseSubscriber;
import com.rain.shoppingscienceproj.http.ExceptionHandle;
import com.rain.shoppingscienceproj.http.RetrofitClient;
import com.rain.shoppingscienceproj.module.Goods;
import com.rain.shoppingscienceproj.ui.activity.BaseActivity;
import com.rain.shoppingscienceproj.ui.activity.shopping.ShoppingDetailsActivity;
import com.rain.shoppingscienceproj.ui.adapter.GoodsAdapter;
import com.rain.shoppingscienceproj.ui.utile.TitleUtils;
import com.rain.shoppingscienceproj.utils.AppManager;
import com.rain.shoppingscienceproj.utils.LoadingDialog;
import com.rain.shoppingscienceproj.utils.toast.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchActivity extends BaseActivity {


    @BindView(R.id.rv_list)
    RecyclerView mRecyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.ll_no_data)
    LinearLayout ll_no_data;
    private GoodsAdapter adapter;
    private List<Goods> goodsList = new ArrayList<>();
    private int type;//首页是1/省份进入2/省份分类进入3
    private String provinceId, typeId, queryString;//省份ID 分类id 查询内容
    private int page = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        AppManager.getAppManager().addActivity(this);
        ButterKnife.bind(this);
        TitleUtils.TitleView(this, "商品搜索");
        type = getIntent().getIntExtra("type", 0);
        queryString = getIntent().getStringExtra("searchString");
        if (type == 2) {
            provinceId = getIntent().getStringExtra("provinceId");
        } else if (type == 3) {
            provinceId = getIntent().getStringExtra("provinceId");
            typeId = getIntent().getStringExtra("typeId");
        }
        loadingDialog = new LoadingDialog(this, "正在加载...");
        initView();
        initData();
        initListener();
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(refreshlayout -> refresh());
        refreshLayout.setOnLoadmoreListener(refreshlayout -> load());
        adapter.setItemClickListener(shopId -> {
            startActivitys(ShoppingDetailsActivity.class, shopId);
        });
    }

    private void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new GoodsAdapter(this, 1);
        mRecyclerView.setAdapter(adapter);
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadmore(true);
    }

    private void refresh() {
        page = 1;
        goodsList = new ArrayList<>();
        initData();
        refreshLayout.finishRefresh();
    }

    private void load() {
        page++;
        initData();
        refreshLayout.finishLoadmore();
    }

    private void initData() {
        Map<String, String> map = new HashMap<>();
        map.put("goodsName", queryString);
        map.put("page", page + "");
        if (type == 2) {
            map.put("provinceId", provinceId);
        } else if (type == 3) {
            map.put("provinceId", provinceId);
            map.put("classId", typeId);
        }
        loadingDialog.show();
        RetrofitClient.getInstance(this)
                .createBaseApi()
                .typeGoods(map, new BaseSubscriber<List<Goods>>(this) {
                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable e) {
                        ToastUtils.showLongToast("网络错误");
                        loadingDialog.dismiss();
                        ll_no_data.setVisibility(View.VISIBLE);
                        refreshLayout.setVisibility(View.GONE);
                    }

                    @Override
                    public void onNext(List<Goods> goods) {
                        super.onNext(goods);
                        loadingDialog.dismiss();
                        if (page == 1) {
                            if (goods.size() == 0) {
                                ll_no_data.setVisibility(View.VISIBLE);
                                refreshLayout.setVisibility(View.GONE);
                            } else {
                                ll_no_data.setVisibility(View.GONE);
                                refreshLayout.setVisibility(View.VISIBLE);
                            }
                        } else {
                            if (goods.size() == 0) {
                                ToastUtils.showShortToast("暂无更多数据了...");
                            }
                        }
                        goodsList.addAll(goods);
                        adapter.setList(goodsList);
                    }
                });

    }
}
